package file;

public class RemoteSharedFile implements Comparable<RemoteSharedFile> {
	String name;
	String digest;
	private String ip;
	String kew_word;

	public RemoteSharedFile(String name, String digest, String ip, String kew_word) {
		this.name = name;
		this.digest = digest;
		this.kew_word = kew_word;
		this.setIp(ip);
	}

	public String name() {
		return this.name;
	}

	public long length() {
		return 0;
	}

	public String lastModified() {
		return "";
	}

	public String abstraction() {
		return digest;
	}

	/**
	 * @param ip the ip to set
	 */
	void setIp(String ip) {
		this.ip = ip;
	}

	/**
	 * @return the ip
	 */
	public String getIp() {
		return ip;
	}
	
	private int _keywordMatch = -1;
	
	/**
	 * How many times does the keyword appears in the digest?
	 * @return number of keyword appearance in digest.
	 */
	public int keywordMatch() {
		if(_keywordMatch == -1)
		{
			_keywordMatch = 0;
			int fromidex = 0;
			int pos = digest.indexOf(this.kew_word, fromidex);
			while(pos != -1)
			{
				_keywordMatch += 1;
				fromidex = pos + 1;
				pos = digest.indexOf(this.kew_word, fromidex);
			}
		}
		
		return _keywordMatch;
	}

	@Override
	public int compareTo(RemoteSharedFile o) {
		return o.keywordMatch() - this.keywordMatch();
	}
}
